package com.spi.soconversion.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import com.spi.soconversion.Batch;
import com.spi.soconversion.base.SOCException;
import com.spi.soconversion.util.DBConnection;
import com.spi.soconversion.util.ErrorConstants;
import com.spi.soconversion.util.ErrorMessageReader;
import com.spi.soconversion.util.QueryConstants;
import com.spi.soconversion.util.QueryReader;
import com.spi.soconversion.util.SOCConstants;
import com.spi.soconversion.util.SOCUtility;

public class ConversionDAOImpl implements ConversionDAO { 
	
	static private org.apache.commons.logging.Log log = 
	    org.apache.commons.logging.LogFactory.getLog(ConversionDAOImpl.class);
		
	public ConversionDAOImpl() {
				 
	}

	public void updateConvertedOrdersInfo(ArrayList<Long> convertedOrdersList,
			ArrayList<Long> notConvertedOrdersList, Batch batch) throws SOCException {
		log.info("Updating the converted orders information...");
		boolean updateFlag= false;
		String orderInfoUpdateQuery = QueryReader.getQuery(QueryConstants.INSERT_CONVERTED_OPEN_ORDERS);
		PreparedStatement orderInfoStmt=null;
		try {
			DBConnection.getConnection().setAutoCommit(false);
			orderInfoStmt=DBConnection.getConnection().prepareStatement(orderInfoUpdateQuery);
		}
		catch (SQLException se) {
//			d_FileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCErrorMessageReader.getError(
//					SOCErrorConstants.FAILED_CREATE_STMT_CONVERTED_OPEN_SALES_ORDERS)+SOCConstants.NEW_LINE+se.getMessage()));
			log.error(SOCUtility.writeLogMessage(ErrorMessageReader.getError(
					ErrorConstants.FAILED_CREATE_STMT_CONVERTED_OPEN_SALES_ORDERS)+SOCConstants.NEW_LINE+se.getMessage()));
			throw new SOCException(
					ErrorMessageReader.getError(
							ErrorConstants.FAILED_CREATE_STMT_CONVERTED_OPEN_SALES_ORDERS),se);
		}	   

		if(convertedOrdersList!=null && convertedOrdersList.size()>0) {
			Iterator<Long> iter=convertedOrdersList.iterator();

			while(iter.hasNext()) {	
				try {
					orderInfoStmt.setLong(1,iter.next());
					orderInfoStmt.setInt(2,batch.getBatchNumber());
					orderInfoStmt.setString(3,SOCConstants.FLAG_Y);
					orderInfoStmt.addBatch();
				} catch (SQLException se) {
//					d_FileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCErrorMessageReader.getError(
//							SOCErrorConstants.FAILED_ADDING_BATCH_CONVERTED_OPEN_SALES_ORDERS)+SOCConstants.NEW_LINE+se.getMessage()));
					log.error(SOCUtility.writeLogMessage(ErrorMessageReader.getError(
							ErrorConstants.FAILED_ADDING_BATCH_CONVERTED_OPEN_SALES_ORDERS)+SOCConstants.NEW_LINE+se.getMessage()));
					throw new SOCException(
							ErrorMessageReader.getError(
									ErrorConstants.FAILED_ADDING_BATCH_CONVERTED_OPEN_SALES_ORDERS),se);
				}
				updateFlag=true;
			}
		}

		if(notConvertedOrdersList!=null && notConvertedOrdersList.size()>0) {
			Iterator<Long> iter=notConvertedOrdersList.iterator();

			while(iter.hasNext()) {	
				try {
					orderInfoStmt.setLong(1,iter.next());
					orderInfoStmt.setInt(2,batch.getBatchNumber());
					orderInfoStmt.setString(3,SOCConstants.FLAG_N);
					orderInfoStmt.addBatch();
				} catch (SQLException se) {
//					d_FileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCErrorMessageReader.getError(
//							SOCErrorConstants.FAILED_ADDING_BATCH_CONVERTED_OPEN_SALES_ORDERS)+SOCConstants.NEW_LINE+se.getMessage()));
					log.error(SOCUtility.writeLogMessage(ErrorMessageReader.getError(
							ErrorConstants.FAILED_ADDING_BATCH_CONVERTED_OPEN_SALES_ORDERS)+SOCConstants.NEW_LINE+se.getMessage()));
					throw new SOCException(
							ErrorMessageReader.getError(
									ErrorConstants.FAILED_ADDING_BATCH_CONVERTED_OPEN_SALES_ORDERS),se);
				}
				updateFlag=true;
			}
		}
		if(updateFlag) {
			try {
				orderInfoStmt.executeBatch();
				DBConnection.getConnection().commit();
				DBConnection.getConnection().setAutoCommit(true);
			
			} catch (SQLException se) {
				se.printStackTrace();
//				d_FileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCErrorMessageReader.getError(
//						SOCErrorConstants.FAILED_UPDATE_BATCH_CONVERTED_OPEN_SALES_ORDERS)+SOCConstants.NEW_LINE+se.getMessage()));
				log.error(SOCUtility.writeLogMessage(ErrorMessageReader.getError(
						ErrorConstants.FAILED_UPDATE_BATCH_CONVERTED_OPEN_SALES_ORDERS)+SOCConstants.NEW_LINE+se.getMessage()));
				throw new SOCException(
						ErrorMessageReader.getError(
								ErrorConstants.FAILED_UPDATE_BATCH_CONVERTED_OPEN_SALES_ORDERS),se);
			}
		}
	}	
}